package com.ruoyi.crm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.crm.domain.CrmFeeApply;
import com.ruoyi.crm.service.ICrmFeeApplyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 费用申请Controller
 *
 * @author yanwenliang
 * @date 2024-10-28
 */
@RestController
@RequestMapping("/crm/apply")
public class CrmFeeApplyController extends BaseController
{
    @Autowired
    private ICrmFeeApplyService crmFeeApplyService;

    /**
     * 查询费用申请列表
     */
    @PreAuthorize("@ss.hasPermi('crm:apply:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmFeeApply crmFeeApply)
    {
        startPage();
        List<CrmFeeApply> list = crmFeeApplyService.selectCrmFeeApplyList(crmFeeApply);
        return getDataTable(list);
    }

    /**
     * 导出费用申请列表
     */
    @PreAuthorize("@ss.hasPermi('crm:apply:export')")
    @Log(title = "费用申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmFeeApply crmFeeApply)
    {
        List<CrmFeeApply> list = crmFeeApplyService.selectCrmFeeApplyList(crmFeeApply);
        ExcelUtil<CrmFeeApply> util = new ExcelUtil<CrmFeeApply>(CrmFeeApply.class);
        util.exportExcel(response, list, "费用申请数据");
    }

    /**
     * 获取费用申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('crm:apply:query')")
    @GetMapping(value = "/{applyId}")
    public AjaxResult getInfo(@PathVariable("applyId") String applyId)
    {
        return success(crmFeeApplyService.selectCrmFeeApplyByApplyId(applyId));
    }

    /**
     * 通过商机id和创建人或者归属人查询费用信息
     * @return
     */
    @GetMapping("selectCrmFeeApplyByOrderIdAndCreateBy/{orderId}")
    public AjaxResult selectCrmFeeApplyByOrderIdAndCreateBy(@PathVariable String orderId){
        return AjaxResult.success(crmFeeApplyService.selectCrmFeeApplyByOrderIdAndCreateBy(orderId));
    }

    /**
     * 新增费用申请
     */
    @PreAuthorize("@ss.hasPermi('crm:apply:add')")
    @Log(title = "费用申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmFeeApply crmFeeApply)
    {
        return toAjax(crmFeeApplyService.insertCrmFeeApply(crmFeeApply));
    }

    /**
     * 修改费用申请
     */
    @PreAuthorize("@ss.hasPermi('crm:apply:edit')")
    @Log(title = "费用申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmFeeApply crmFeeApply)
    {
        return toAjax(crmFeeApplyService.updateCrmFeeApply(crmFeeApply));
    }

    /**
     * 删除费用申请
     */
    @PreAuthorize("@ss.hasPermi('crm:apply:remove')")
    @Log(title = "费用申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{applyIds}")
    public AjaxResult remove(@PathVariable String[] applyIds)
    {
        return toAjax(crmFeeApplyService.deleteCrmFeeApplyByApplyIds(applyIds));
    }

    /**
     * 提交
     *
     * @param crmFeeApply
     * @return
     */
    @PostMapping("submit")
    public AjaxResult submit(@RequestBody CrmFeeApply crmFeeApply) {
        return AjaxResult.success(crmFeeApplyService.submit(crmFeeApply));
    }

    /**
     * 回撤提交
     *
     * @param crmFeeApply
     * @return
     */
    @PostMapping("cancelInfo")
    public AjaxResult cancelInfo(@RequestBody CrmFeeApply crmFeeApply) {
        return AjaxResult.success(crmFeeApplyService.cancelInfo(crmFeeApply));
    }

    /**
     * 审核
     *
     * @param crmFeeApply
     * @return
     */
    @PostMapping("audit")
    public AjaxResult audit(@RequestBody CrmFeeApply crmFeeApply) {
        return AjaxResult.success(crmFeeApplyService.audit(crmFeeApply));
    }



}
